import { defineStore, storeToRefs } from 'pinia';
import { getBullPageApi, invoiceListApi, unitPriceApi } from '/@/api/comprehensive/monthBoll';
import { messageFn } from '../../utils';
import { comprehensiveStore } from './home';

const comprehensive = comprehensiveStore();
const { ckeckList } = storeToRefs(comprehensive);

interface monthbilltype {
  monthlist: any[];
  costlist: any[];
  selectlist: any[];
  selectRows: any[];
  total: '';
  content: any[];
  invoiceTyoelist: any[]; //发票类型
  special_type:number;//特殊处理类型
  titleType: string; //查缴水费title的名称
}

export const monthbillStore = defineStore('monthbill_store', {
  state: (): monthbilltype => {
    return {
      // 月份账单列表
      monthlist: [],
      // 费用列表
      costlist: [],
      // 存储选择行的信息
      selectlist: [],
      selectRows: [],
      total: '',
      content: [],
      invoiceTyoelist: [], //发票类型
      special_type: 0,//特殊处理类型 (1-费用减免;2-违约金减免;3-追收账单;4-冲减处理)
      titleType : '账单费用',
    };
  },

  actions: {
    // 获得发票类型数据列表
    async getinvoiceTyoelist() {
      const { code, data, msg } = await invoiceListApi();
      console.log(data, 'datadata');

      if (code === 0) {
        this.invoiceTyoelist = data;
      }
    },

    // 获得计费单价
    async getPrice() {
      const res = await unitPriceApi();
      console.log(res, 'res');
    },
    // 存储账单列表
    async setmonthlist(obj?: any) {
      // console.log(obj, 'obj');
      const params = {
        user_ids: ckeckList.value,
        ...obj,
      };
      const { code, msg, data } = await getBullPageApi(params);
      // console.log(data, 'data');
      if (code === 0) {
        if (data === null) {
          this.monthlist = [];
          return;
        }
        // console.log(data.page_result.total, 'data.page_result');

        this.total = data.page_result.total;
        // console.log(this.total, 'total');

        this.monthlist = data.page_result.list || [];
        this.monthlist.forEach((item) => {
          item.check = false;
        });
        return data.page_result;
      } else {
        messageFn('error', msg);
      }
    },
    // 存储费用列表
    setcostlist(data) {
      this.costlist = data;
    },

    setselectlist(data) {
      this.selectlist = data;
    },
    setmonthData(data) {
      this.monthlist = data;
    },
    //特殊处理类型存储 //特殊处理类型 (1-费用减免;2-违约金减免;3-追收账单;4-冲减处理)
    setSpecialType(type:number){
      this.special_type = type;
      if(type === 0){
        this.titleType = '账单费用';
      }
      if(type === 1 || type === 2){
        this.titleType = '待减免账单';
      }
      if(type === 4){
        this.titleType = '待冲减账单';
      }
      if(type === 5 || type === 6){
        this.titleType = '待费用返还';
      }
    },

    // 把selectlist赋值为空
    setselect() {
      this.selectlist = [];
    },

    // 存储卡片信息
    async setcardList(dataObj) {
      const params = {
        user_ids: ckeckList.value,
        ...dataObj,
      };
      return getBullPageApi(params);
    },

    setcontent(data) {
      this.content = data;
    },
  },
});
